package logic
{
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	public class MenuBarLogic extends Logic
	{
		public function MenuBarLogic()
		{
			super();
		}
		
	    override protected function onCreationCompleteHandler(event:FlexEvent):void {
	    	trace(this + "onCreationCompleteHandler(event=" + event + ")");
	    	//this.buildMenu();
	    }

		public function buildMenu():void {
			trace(this + ":buildMenu()");
			// ネイティブメニューを構築します
	        var rootMenu:NativeMenu = new NativeMenu();
	        var fileMenu:NativeMenuItem = rootMenu.addSubmenu(new NativeMenu(), "File");
	        {
    		    var analizeMenu:NativeMenuItem = new NativeMenuItem("Analyze");
  	    		analizeMenu.addEventListener(Event.SELECT, onAnalizeSelect);
        		fileMenu.submenu.addItem(analizeMenu);

    		    var openMenu:NativeMenuItem = new NativeMenuItem("Open...");
  	    		openMenu.addEventListener(Event.SELECT, onOpenSelect);
        		fileMenu.submenu.addItem(openMenu);
        		
    		    var exitMenu:NativeMenuItem = new NativeMenuItem("Exit");
   	    		exitMenu.addEventListener(Event.SELECT, onExitSelect);
        		fileMenu.submenu.addItem(exitMenu);
	        }
			// 構築したメニューを設定します
   			super.document.stage.nativeWindow.menu = rootMenu;
      	}
    //--------------------------------------
   	// UI Event Handler
   	//--------------------------------------
   	/**
   	 * アプリケーションを終了します
   	 */
   	public function onExitSelect(event:Event):void {
   		this.sbvaEditor.exit();
   	}

   	public function onOpenSelect(event:Event):void {
   		var file:File = File.userDirectory;
   		file.addEventListener(Event.SELECT, onReadSelect);
   		var filter:FileFilter = new FileFilter("SBVA Scenario (*.xml)","*.xml");
   		file.browseForOpen("Select a scenario file",[filter]);
   	}
   	
   	private function onReadSelect(event:Event):void {
   		try {
   			// ファイルストリームからファイルを読み込みます
   			var file:File = event.target as File; 
   			var fs:FileStream = new FileStream();
   			fs.open(file,FileMode.READ);
   			var s:String = fs.readMultiByte(file.size, "UTF-8");
   			//var s:String = fs.readMultiByte(file.size, "ShiftJIS");
   			fs.close();
   			
   			// XMLに変換し，SBVAEditorのモデルに設定します
   			trace("Scenario XML has read.");
   			this.sbvaEditor.model = new XML(s);
   		} catch(e:Error) {
  				trace(s);
  				Alert.show(e.toString());
   		}
   	}
   	
   	private function onAnalizeSelect(event:Event):void {
   		this.sbvaEditor.sbvaEditorLogic.model = this.sbvaEditor.model;
   	}
	   	
   		/**
   		 * プロパティ
   		 */
   	
	   	// アプリケーションの参照用（ダウンキャスト）
   		private var _sbvaEditor:SBVAEditor; // UIComponentからダウンキャストして保存します．
   	
		public function get sbvaEditor():SBVAEditor {
			if(this._sbvaEditor == null) {
				this._sbvaEditor = super.document as SBVAEditor; 
			}
       		return this._sbvaEditor;
		}
	}
}